<?php
	class Offer extends Model_Abstract{
		
		public function add($name, $availability, $type, $status, $price, $convPos, $ipWindow, $cookieWindow){
			try{
				$status = 0;
				$sql = 'insert into offers set name=?, availability=?, type=?, status=?, price=?, conv_pos=?, ip_window=?, cookie_window=?';
				$result = $this->_connection->execute($sql, array($name, $availability, $type, $status, $price, $convPos, $ipWindow, $cookieWindow), true);
				return $result;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function update($name, $availability, $ipWindow, $cookieWindow, $id){
			try{
				$sql = 'update offers set name=?, availability=?, ip_window=?, cookie_window=? where id=?';
				$result = $this->_connection->execute($sql, array($name, $availability, $ipWindow, $cookieWindow, $id));
				return $result;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function getOfferForUpdating($id){
			try{
				$sql = 'select id, name, availability, ip_window, cookie_window from offers where id=?';
				$result = $this->_connection->query($sql, array($id));
				if($result)
					return $result[0];
				return false;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function delete($id){
			try{
				$sql = 'delete from offers where id=?';
				$result = $this->_connection->execute($sql, array($id));
				return $result;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function getStatus($offerId){
			try{
				$sql = 'select status from offers where id=?';
				$result = $this->_connection->query($sql, array($offerId));
				if($result)
					return $result[0]['status'];
				return false;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function getOfferById($id){
			try{
				$sql = 'select * from offers where id=?';
				$result = $this->_connection->query($sql, array($id));
				if($result)
					return $result[0];
				return false;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function getAllOffers(){
			try{
				$sql = 'SELECT o.id, o.name as offer_name, o.availability, o.status as status, o.type, o.price, cre.size, cre.url as creative_url, cre.id as creative_id,  
					lu.url as landing_url, lu.id as landing_id, c.name as campaign_name, c.id as campaign_id
					FROM offers AS o LEFT JOIN o_cre_refs AS oc ON o.id = oc.o_id LEFT JOIN creatives AS cre ON oc.cre_id = cre.id
					LEFT JOIN o_lu_refs AS ol ON o.id = ol.o_id LEFT JOIN landing_urls AS lu ON ol.lu_id = lu.id LEFT JOIN campaigns AS c ON o.id = c.o_id';
				$result = $this->_connection->query($sql);
				return $result;;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		private function canSetActive($id){
			try{
				$sql = 'SELECT o . * , c . * , l . * FROM offers AS o JOIN o_cre_refs AS c ON o.id = c.o_id JOIN o_lu_refs AS l ON o.id = l.o_id';
				$result = $this->_connection->query($sql);
				if($result)
					return true;
				return false;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function cloneOffer($id, $name, $availability, $type, $status, $price, $cov_pos, $ip_window, $cook_window){
			$oldOffer = $this->getOfferById($id);
			$this->_connection->beginTransactionW();
			$newOfferId = $this->add($name, $availability, $type, $status, $price, $cov_pos, $ip_window, $cook_window);
			$ref_oc = new Ref_Oc();
			if($newOfferId){
				$flag = $ref_oc->cloneRef($oldOffer['id'], $newOfferId);
				if($flag){
					$ref_ol = new Ref_Ol();
					$flag = $ref_ol->cloneRef($oldOffer['id'], $newOfferId);
				}
			}
			if($flag){
				$this->_connection->commitW();
				$oldOffer['id'] = $newOfferId;
				return $oldOffer;
			}
			$this->_connection->rollBackW();
			return false;			
		}
		
		public function canPauseOrDelete($id){
			try{
				$sql = 'select count(*) from campaigns where o_id=?';
				$result = $this->_connection->query($sql, array($id));
				if($result && $result >= 1)
					return true;
				return false;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function getOffersAsDetailedList(){
			try{
				$sql = 'select id, name, type, price from offers where availability=0';
				$result = $this->_connection->query($sql);
				if($result)
					return $result;
				return false;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function getOffersAsList(){
			try{
				$sql = 'select id, name from offers';
				$result = $this->_connection->query($sql);
				if($result)
					return $result;
				return false;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		
		public function search($name, $availability = '', $status = '', $type = ''){
			try{
				$sql = 'SELECT o.id, o.name as offer_name, o.availability, o.status as status, o.type, o.price, cre.size, cre.url as creative_url, cre.id as creative_id,  
					lu.url as landing_url, lu.id as landing_id, c.name as campaign_name, c.id as campaign_id
					FROM offers AS o LEFT JOIN o_cre_refs AS oc ON o.id = oc.o_id LEFT JOIN creatives AS cre ON oc.cre_id = cre.id
					LEFT JOIN o_lu_refs AS ol ON o.id = ol.o_id LEFT JOIN landing_urls AS lu ON ol.lu_id = lu.id LEFT JOIN campaigns AS c ON o.id = c.o_id where o.name like ? ';
				$vars = array("%" . trim($name). "%");
				$conditions = array();
				if('' != $availability){
					$conditions['o.availability'] = $availability;
				}
				if('' != $status){
					$conditions['o.status'] = $status;
				}
				if('' != $type){
					$conditions['o.type'] = $type;
				}
				if(count($conditions) > 0){
					foreach ($conditions as $k=>$v){
						$sql .= "and $k=?";
						array_push($vars, $v);
					}
				}
				return $this->_connection->query($sql, $vars);
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function setActive($id){
			try{
				$sql = 'update offers set status=1 where id=?';
				$result = $this->_connection->execute($sql, array($id));
				return $result;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
		public function setPause($id){
			try{
				$sql = 'update offers set status=2 where id=?';
				$result = $this->_connection->execute($sql, array($id));
				return $result;
			}catch (Exception $e){
				QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
				throw new Model_Exception($e->getMessage(), $e->getCode());
			}
		}
		
	}